import request from '@/utils/request';
import type { Result, PageResult } from '@/types/global';
import axios from 'axios';

// 充电订单列表查询参数接口
export interface OrderRefundQueryParams {
  pageSize?: number;
  pageNum?: number;
  orderNumber?: string;
  mobile?: string;
  status?: number;
  refundStartDate?: string;
  refundEndDate?: string;
}

// 充电订单实体接口
export interface RefundOrder {
  id: number;
  orderNumber: string;
  sn?: string;
  money?: number;
  actualMoney?: number;
  memberId?: number;
  mobile?: string;
  orderType?: number;
  merchantId?: string;
  method?: string;
  status?: number;
  createBy?: string;
  createDate?: string;
  updateBy?: string;
  updateDate?: string;
  memo?: string;
  opMobile?: string;
  refundDate?: string;
  isDivide?: number;
}

// 获取充电订单列表
export function getRefundOrderList(params: OrderRefundQueryParams) {
  return request.get<{
    records: RefundOrder[];
    total: number;
    size: number;
    current: number;
  }>('/api/pay/refund/page', params);
}

// 获取充电订单详情
export function getChargingOrderDetail(id: string | number) {
  return request.get<RefundOrder>(`/api/order/${id}`);
}

// 支付状态枚举
export const PaymentStatusEnum = {
  UNPAID: 0,
  PAID: 1,
  REFUNDED: 2,
  PARTIAL_REFUNDED: 3,
} as const;

// 订单状态枚举
export const PayOrderRefundstatusEnums = {
  WAITING: 0, // 待退款
  SUCCESS: 1, // 退款成功
  FAILED: 2, // 退款失败
  REFUNDING: 3, // 退款中
} as const;

// 支付方式枚举
export const PaymentMethodEnum = {
  WECHAT: 1,
  ALIPAY: 2,
  UNIONPAY: 3,
  BALANCE: 4,
} as const;

// 停止原因枚举
export const StopReasonEnum = {
  NORMAL: 1,
  MANUAL: 2,
  SYSTEM_ERROR: 3,
  TIMEOUT: 4,
  INSUFFICIENT_BALANCE: 5,
} as const;

// 订单统计数据接口
export interface PayOrderRefundStatistics {
  totalCount: number;
  totalRefundAmount: number;
}

// 获取充电订单统计数据
export function getRefundOrderStatistics(params: OrderRefundQueryParams) {
  return axios.get<PayOrderRefundStatistics>('/api/pay/refund/statistics', {
    params,
  });
}

/**
 * 充电桩列表查询参数
 */
export interface ChargingListParams {
  pageNum: number;
  pageSize: number;
  code?: string;
  name?: string;
  chargingGroupId?: number;
  status?: number;
}

/**
 * 充电桩数据
 */
export interface ChargingData {
  id: number;
  code: string;
  name: string;
  socketCount: number;
  chargingGroupId?: number;
  chargingGroupName?: string;
  status: number; // 0: 离线, 1: 在线
}

/**
 * 获取充电桩列表
 */
export function getChargingList(params: ChargingListParams) {
  return axios.get<Result<PageResult<ChargingData>>>('/api/charging/list', {
    params,
  });
}

/**
 * 新增充电桩
 */
export function addCharging(data: Omit<ChargingData, 'id'>) {
  return axios.post<Result>('/api/charging', data);
}

/**
 * 更新充电桩
 */
export function updateCharging(data: ChargingData) {
  return axios.put<Result>('/api/charging', data);
}

/**
 * 删除充电桩
 */
export function deleteCharging(id: number) {
  return axios.delete<Result>(`/api/charging/${id}`);
}

/**
 * 停止充电
 */
export function stopCharging(thirdPartyOrderNo: string | undefined) {
  if (!thirdPartyOrderNo) {
    return Promise.reject(new Error('订单号不能为空'));
  }
  return request.post<string>('/api/order/endCharge', {
    thirdPartyOrderNo,
  });
}

/**
 * 批量导入充电桩
 */
export function importChargingPiles(
  data: Array<{
    code: string;
    name: string;
    socketCount: number;
    chargingGroupId?: number;
  }>
) {
  return axios.post<Result<void>>('/api/charging/import', data);
}

/**
 * 订单退款
 */
export function refundOrder(
  thirdPartyOrderNo: string,
  refundAmount: string,
  reason?: string
) {
  return request.post<string>('/api/order/refund', {
    thirdPartyOrderNo,
    refundAmount,
    reason,
  });
}
